import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:nft_shop/box/model/models.dart';
import 'package:nft_shop/box/widget/box_open_widget.dart';
import 'package:nft_shop/net/dio_api.dart';
import 'package:nft_shop/net/service_repository.dart';
import 'package:nft_shop/res/constant.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/util/other_utils.dart';
import 'package:nft_shop/widgets/load_image.dart';
import 'package:nft_shop/widgets/my_app_bar.dart';
import 'package:nft_shop/widgets/my_button.dart';
import 'package:nft_shop/widgets/my_scroll_view.dart';
import 'package:nft_shop/widgets/status_view.dart';

class SplitBoxPage extends StatefulWidget {
  String goodid;
  String id;
  bool? isSingle;

  SplitBoxPage(
      {Key? key, required this.goodid, required this.id, this.isSingle})
      : super(key: key);

  @override
  _SplitBoxPageState createState() => _SplitBoxPageState();
}

class _SplitBoxPageState extends State<SplitBoxPage> {
  late BoxInfoModel _boxInfo;

  ///状态
  int _status = LoadStatusType.loading;

  int get status => _status;

  set status(int value) {
    _status = value;
    setState(() {});
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getData();
  }

  Future<void> getData() async {
    try {
      if (widget.isSingle == true) {
        _boxInfo = await ServiceRepository.goodsInfoMang(id: widget.id);
      } else {
        _boxInfo = await ServiceRepository.mangInfo(id: widget.id);
      }
      status = LoadStatusType.success;
      EasyLoading.dismiss();
    } catch (e, s) {
      status = LoadStatusType.empty;
      EasyLoading.showToast(http.setError(e, s));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(48),
        child: AppBar(
          title: Text(
            "拆盲盒",
            style: TextStyle(
                fontSize: 16,
                color: Color(0xFF131415),
                fontFamily: Global.mySemiboldFont),
          ),
          centerTitle: true,
          leading: IconButton(
            padding: const EdgeInsets.only(right: 12, top: 12, bottom: 12),
            icon: Image.asset(
              'assets/images/ic_back_black.png',
            ),
            onPressed: () {
              if (widget.isSingle == true) {
                Get.back();
              } else {
                Navigator.of(Get.context!)
                  ..pop()
                  ..pop()
                  ..pop();
              }
            },
          ),
        ),
      ),
      body: Builder(builder: (context) {
        if (status != LoadStatusType.success) {
          return StatusViews(status);
        }
        return MyScrollView(
          children: [
            specialTopView(),
            Container(
              height: 5,
              color: Global.kTheme.colorLine,
            ),
            boxSplit(),
            Container(
              height: 5,
              color: Global.kTheme.colorLine,
            ),
            extractingRecord(),
          ],
        );
      }),
    );
  }

  //专题信息
  Widget specialTopView() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 15, vertical: 15),
      child: Row(
        children: [
          LoadImage(
            _boxInfo.info!.thumb ?? "",
            width: 80,
            height: 80,
          ),
          SizedBox(
            width: 13,
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                _boxInfo.info!.title ?? "",
                style: TextStyle(
                    fontSize: 14,
                    color: Global.kTheme.textPrimary4,
                    fontFamily: Global.mySemiboldFont),
              ),
              SizedBox(
                height: 20,
              ),
              Row(
                children: [
                  issueView("发行", _boxInfo.info!.allCount.toString()),
                  SizedBox(
                    width: 5,
                  ),
                  issueView("已售", _boxInfo.info!.saleCount.toString()),
                ],
              )
            ],
          ),
        ],
      ),
    );
  }

  //拆盲盒
  Widget boxSplit() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 28, vertical: 20),
      child: Column(
        children: [
          ClipRRect(
              borderRadius: BorderRadius.circular(8),
              child: LoadImage(
                _boxInfo.info!.thumb ?? "",
                width: double.infinity,
                height: (Get.width - 56) * 299 / 320,
              )),
          SizedBox(
            height: 20,
          ),
          Container(
            height: 40,
            child: Row(
              children: [
                Expanded(
                  flex: 1,
                  child: Container(
                    height: 40,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(4),
                    ),
                    child: MyButton(
                      minHeight: 40,
                      onPressed: () {
                        if (widget.isSingle == true) {
                          Get.back();
                        } else {
                          Navigator.of(Get.context!)
                            ..pop()
                            ..pop()
                            ..pop();
                        }
                      },
                      text: "暂不拆开",
                      fontSize: 16,
                      textColor: Global.kTheme.textPrimary4,
                      backgroundColor: Color(0xFFEFEFEF),
                    ),
                  ),
                ),
                SizedBox(
                  width: 20,
                ),
                Expanded(
                  flex: 1,
                  child: Container(
                    height: 40,
                    decoration: BoxDecoration(
                      color: Global.kTheme.colorBackground3,
                      borderRadius: BorderRadius.circular(4),
                    ),
                    child: MyButton(
                      minHeight: 40,
                      onPressed: () async {
                        OpenBoxModel? openModel;
                        try {
                          openModel = await ServiceRepository.openBlindBox(
                              id: widget.goodid);
                        } catch (s, e) {}
                        if (openModel != null) {
                          Utils.showElasticDialog<void>(
                            context: context,
                            barrierDismissible: false,
                            builder: (BuildContext context) {
                              return BoxOpenDialog(
                                model: openModel!,
                                isSingle: widget.isSingle,
                              );
                            },
                          );
                        }
                      },
                      text: "立即拆开",
                      fontSize: 16,
                      textColor: Colors.white,
                      backgroundColor: Global.kTheme.colorBackground3,
                    ),
                  ),
                ),
              ],
            ),
          ),
          SizedBox(
            height: 6,
          ),
        ],
      ),
    );
  }

  //抽取记录
  Widget extractingRecord() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 13),
      child: Column(
        children: [
          InkWell(
            child: Container(
              height: 40,
              child: Row(
                children: [
                  Text(
                    "抽取记录",
                    style: TextStyle(
                        fontSize: 14,
                        color: Global.kTheme.textPrimary1,
                        fontFamily: Global.mySemiboldFont),
                  ),
                  Spacer(),
                  Text(
                    "更多抽取记录",
                    style: TextStyle(
                        fontFamily: Global.mySemiboldFont,
                        fontSize: 12,
                        color: Global.kTheme.textPrimary3),
                  ),
                  SizedBox(
                    width: 5,
                  ),
                  LoadAssetImage(
                    "icon_arrows",
                    width: 12,
                    height: 12,
                  ),
                ],
              ),
            ),
          ),
          // Expanded(child: Builder(builder: (context) {
          //   if (_boxInfo.specialBlindBoxLog!.length == 0) {
          //     return StatusViews(LoadStatusType.empty);
          //   }
          //   return ListView.builder(
          //       physics: NeverScrollableScrollPhysics(), //禁止滑动
          //       itemCount: _boxInfo.specialBlindBoxLog!.length > 3
          //           ? 3
          //           : _boxInfo.specialBlindBoxLog!.length,
          //       itemBuilder: (context, int index) {
          //         return transactionItem(
          //             _boxInfo.specialBlindBoxLog![index]);
          //       });
          // })),
        ],
      ),
    );
  }

  Widget transactionItem(SpecialBlindBoxLog model) {
    return Container(
      padding: EdgeInsets.only(
        top: 15,
        bottom: 13,
      ),
      decoration: BoxDecoration(
          border: Border(
        bottom: Divider.createBorderSide(context,
            color: Global.kTheme.colorLine, width: 1),
      )),
      child: Row(
        children: [
          Column(
            children: [
              Text(
                model.nickname ?? "",
                style: TextStyle(
                    fontSize: 12,
                    color: Global.kTheme.textPrimary1,
                    fontFamily: Global.mySemiboldFont),
              ),
              SizedBox(
                height: 1,
              ),
              Text(
                model.createTime ?? "",
                style: TextStyle(
                    fontSize: 12,
                    color: Global.kTheme.textPrimary3,
                    fontFamily: Global.mySemiboldFont),
              ),
            ],
          ),
          Spacer(),
          Text(
            "抽中${model.title}",
            style: TextStyle(
                fontSize: 12,
                color: Global.kTheme.textPrimary1,
                fontFamily: Global.mySemiboldFont),
          ),
        ],
      ),
    );
  }

  //发行
  Widget issueView(String left, String right) {
    return Row(
      children: [
        Container(
          color: Global.kTheme.colorBackground3,
          padding: EdgeInsets.symmetric(horizontal: 5, vertical: 2),
          child: Text(
            left,
            style: TextStyle(
                fontSize: 10,
                color: Global.kTheme.textPrimary5,
                fontFamily: Global.mySemiboldFont),
          ),
        ),
        Container(
          color: Color(0xFFB6B6B6),
          padding: EdgeInsets.symmetric(horizontal: 5, vertical: 2),
          child: Text(
            right,
            style: TextStyle(
                fontSize: 10,
                color: Global.kTheme.textPrimary5,
                fontFamily: Global.mySemiboldFont),
          ),
        ),
      ],
    );
  }
}
